<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Hotproduct_model extends My_Model
{
   
    public function __construct()
    {
        parent::__construct();
        $this->tablename="hot_product";   
    }

    public function decsell($pro)
    {

        $date=date("Y-m-d",strtotime($pro["addtime"]));       
        if(empty($pro["rid"]))
        {
            $pro["rid"]=0;
            $where=["siteid"=>$pro["siteid"],"name"=>$pro["name"],"day"=>$date];
        }
        else
        {
            $where=["siteid"=>$pro["siteid"],"rid"=>$pro["rid"],"day"=>$date];
        }        
        $checkitem=$this->get_item_by_where($where);
        if($checkitem)
        {
            if($checkitem["sellcount"]==$pro["quantity"])
            {
                $this->delete(["id"=>$checkitem["id"]]);
            }
            else
            {
                $this->update($checkitem["id"],["sellcount"=>$checkitem["sellcount"]-$pro["quantity"]]);
            }
        }
        
    }


    public function addsell($pro)
    {
        $date=date("Y-m-d",strtotime($pro["addtime"]));       
        if(empty($pro["rid"]))
        {
            $pro["rid"]=0;
            $where=["siteid"=>$pro["siteid"],"name"=>$pro["name"],"day"=>$date];
        }
        else
        {
            $where=["siteid"=>$pro["siteid"],"rid"=>$pro["rid"],"day"=>$date];
        }        
        $checkitem=$this->get_item_by_where($where);
        if($checkitem)
        {
            $this->update($checkitem["id"],["sellcount"=>$checkitem["sellcount"]+$pro["quantity"]]);
        }
        else
        {
            $data=[
                "siteid"=>$pro["siteid"],
                "rid"=>$pro["rid"],
                "name"=>$pro["name"],
                "thumb"=>$pro["thumb"],
                "price"=>$pro["price"],
                "sellcount"=>$pro["quantity"],
                "siteurl"=>$pro["siteurl"],                
                "day"=>$date,
            ];
            $this->create($data);

        }
    }


    public function tongji($days)
    {   
        
        $where=" datediff ( day , '{$day}' ) = 0";
          
    }


    public function getnill()
    {
        $dayend = date("Y-m-d");
        $daybegin = date("Y-m-d",strtotime("-91 day"));
        $where=" where day between '".$daybegin."' and '".$dayend."'";
        $sql = "select sum(sellcount) as total,name,siteid,thumb,price,rid from ".$this->table_prefix.$this->tablename." {$where} group by siteid,rid having total>3 order by total desc";
        $query = $this->db->query($sql);
        $data = $query->result_array();


        $dayend = date("Y-m-d");
        $daybegin = date("Y-m-d",strtotime("-8 day"));
        $where=" where day between '".$daybegin."' and '".$dayend."'";
        $sql = "select sum(sellcount) as total,name,siteid,thumb,price,rid from ".$this->table_prefix.$this->tablename." {$where} group by siteid,rid having total>0";
        $query = $this->db->query($sql);
        $data2 = $query->result_array();

        foreach($data as $k=>$v)
        {
            $removed = 0;
            foreach($data2 as $v2)
            {
                if($v2["siteid"]==$v["siteid"] && $v2["rid"]==$v["rid"])
                {
                    unset($data[$k]);
                    $removed = 1;
                    break;
                }
            }
            if($removed==0)
            {
                $sql="select day from ".$this->table_prefix.$this->tablename." where rid=".$v["rid"]." order by id desc limit 1";
                $query = $this->db->query($sql);
                $dt=$query->result_array();
                //print_r($dt);die();
                $data[$k]["lastday"]=$dt[0]["day"];
            }
        }

        return $data;
    }


    public function getpage($timelimit,$page,$key="",$siteid=0)
    {
        $where="";
        if($timelimit && $timelimit["start"] && $timelimit["end"])
        {
            $where=" where day between '".$timelimit["start"]."' and '".$timelimit["end"]."'";
        }
        if($siteid>0)
        {
            $where.=" and siteid=".$siteid;
        }
        if($key)
        {
            $where.=" and name like '%".$key."%'";
        }
        $sql="select rid from ".$this->table_prefix.$this->tablename." {$where} group by siteid,rid";
        
        $query = $this->db->query($sql);
        $row = $query->result_array();
        $total=count($row);
        $offset=$this->pagesize*($page-1);
        $sql="select sum(sellcount) as total,name,siteid,thumb,price,siteurl from ".$this->table_prefix.$this->tablename." {$where} group by siteid,rid order by total desc limit ".$offset.",".$this->pagesize;
        $query = $this->db->query($sql);
        $data = $query->result_array();

        $config['base_url']=site_url(uri_string());
        $config['total_rows']=$total;
        $config['per_page']=$this->pagesize;        
        $this->pagination->initialize($config);
        $pager=$this->pagination->create_links();
        $pages=ceil($total/$this->pagesize);  
        return array('data'=>$data,'pager'=>$pager,'total'=>$total,'pages'=>$pages);

    }

    public function update($id,$data)
    {
        if(is_array($id))
        {
            $where="id in (".implode(",",$id).")";
          
        }else{
            $where=array("id"=>$id);
        
        }
        return $this->update_item($where,$data);
    }


    public function create($data){        
        return $this->add_item($data);         
    }
    

}
